#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
/*
 * Zenlayer.com Inc.
 * Copyright (c) 2014-2020 All Rights Reserved.
 */
package ${package}.${artifactId}.data.mapper;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.SqlHelper;
import tk.mybatis.mapper.provider.ExampleProvider;

import java.util.Set;

/**
 * @author yangshen
 * @date 2019-09-02 14:24:07
 * @version ${symbol_dollar} Id: BatchExampleProvider.java, v 0.1  yangshen Exp ${symbol_dollar}
 */
public class BatchExampleProvider extends ExampleProvider {

    public BatchExampleProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    /**
     * 拼update sql, 使用case when方式，id为主键
     *
     * @param ms
     * @return
     */
    public String updateBatchByPrimaryKeySelective(MappedStatement ms) {
        final Class<?> entityClass = getEntityClass(ms);
        //开始拼sql
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
        sql.append("<trim prefix=${symbol_escape}"set${symbol_escape}" suffixOverrides=${symbol_escape}",${symbol_escape}">");

        //获取全部列
        Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
        for (EntityColumn column : columnList) {
            if (!column.isId() && column.isUpdatable()) {
                sql.append("  <trim prefix=${symbol_escape}"").append(column.getColumn()).append(" =case${symbol_escape}" suffix=${symbol_escape}" else ").append(column.getColumn()).append(" end,${symbol_escape}">");
                sql.append("    <foreach collection=${symbol_escape}"list${symbol_escape}" item=${symbol_escape}"i${symbol_escape}" index=${symbol_escape}"index${symbol_escape}">");
                sql.append("      <if test=${symbol_escape}"i.").append(column.getEntityField().getName()).append("!=null${symbol_escape}">");
                sql.append("         when id=${symbol_pound}{i.id} then ${symbol_pound}{i.").append(column.getEntityField().getName()).append("}");
                sql.append("      </if>");
                sql.append("    </foreach>");
                sql.append("  </trim>");
            }
        }

        sql.append("</trim>");
        sql.append("WHERE");
        sql.append(" id IN ");
        sql.append("<trim prefix=${symbol_escape}"(${symbol_escape}" suffix=${symbol_escape}")${symbol_escape}">");
        sql.append("<foreach collection=${symbol_escape}"list${symbol_escape}" separator=${symbol_escape}", ${symbol_escape}" item=${symbol_escape}"i${symbol_escape}" index=${symbol_escape}"index${symbol_escape}" >");
        sql.append("${symbol_pound}{i.id}");
        sql.append("</foreach>");
        sql.append("</trim>");

        return sql.toString();
    }

    public String updateBatchByPrimaryKey(MappedStatement ms) {
        final Class<?> entityClass = getEntityClass(ms);
        //开始拼sql
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
        sql.append("<trim prefix=${symbol_escape}"set${symbol_escape}" suffixOverrides=${symbol_escape}",${symbol_escape}">");
        //获取全部列
        Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
        for (EntityColumn column : columnList) {
            if (!column.isId() && column.isUpdatable()) {
                sql.append("  <trim prefix=${symbol_escape}"").append(column.getColumn()).append(" =case${symbol_escape}" suffix=${symbol_escape}"end,${symbol_escape}">");
                sql.append("    <foreach collection=${symbol_escape}"list${symbol_escape}" item=${symbol_escape}"i${symbol_escape}" index=${symbol_escape}"index${symbol_escape}">");
                sql.append("         when id=${symbol_pound}{i.id} then ${symbol_pound}{i.").append(column.getEntityField().getName()).append("}");
                sql.append("    </foreach>");
                sql.append("  </trim>");
            }
        }
        sql.append("</trim>");
        sql.append("WHERE");
        sql.append(" id IN ");
        sql.append("<trim prefix=${symbol_escape}"(${symbol_escape}" suffix=${symbol_escape}")${symbol_escape}">");
        sql.append("<foreach collection=${symbol_escape}"list${symbol_escape}" separator=${symbol_escape}", ${symbol_escape}" item=${symbol_escape}"i${symbol_escape}" index=${symbol_escape}"index${symbol_escape}" >");
        sql.append("${symbol_pound}{i.id}");
        sql.append("</foreach>");
        sql.append("</trim>");
        return sql.toString();
    }
}
